package com.jmp.api.dto;

import com.jmp.base.MessageApi;
import one.stand.enums.EnumUtil;
import one.stand.service.RecordPublicService;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

public enum WxSubscribeTypeEnum {
    UNREAD_MSG(MessageApi.MSG_UNREAD, "c80M4KMzEseFaD0Vr0eOO-GCemLD48XH4vgACzRWsjg", "pages/live/im/main?toUser=%s", "{\"name1\": {\n" +
            "          \"value\": \"%s\"\n" +
            "      },\n" +
            "      \"time2\": {\n" +
            "          \"value\": \"%s\"\n" +
            "      },\n" +
            "      \"thing3\": {\n" +
            "          \"value\": \"%s\"\n" +
            "      }}"),
    MSG_LIVE_OPEN(MessageApi.MSG_LIVE_OPEN, "Y6TokcuPAA-Fa9mcKJ84FtN2nX2secLwjvY4KFO3NJc", RecordPublicService.WECHAT_SHARE_ROOM, "{\"thing1\": {\n" +
            "          \"value\": \"红木直播开播提醒\"\n" +
            "      },\n" +
            "      \"time2\": {\n" +
            "          \"value\": \"%s\"\n" +
            "      },\n" +
            "      \"thing4\": {\n" +
            "          \"value\": \"您预约的直播已经开播，点击进入\"\n" +
            "      }}"),
    ;

    private Integer id;
    private String templateId;
    private String page;
    private String data;

    WxSubscribeTypeEnum(Integer id, String templateId, String page, String data) {
        this.id = id;
        this.templateId = templateId;
        this.page = page;
        this.data = data;
    }

    public Integer getId() {
        return id;
    }

    public String getTemplateId() {
        return templateId;
    }

    public String getPage(WxSubscribeDto subscribeDto) {
        String pageStr = page;
        if (StringUtils.contains(pageStr, "{toId}")) {
            pageStr = StringUtils.replaceAll(pageStr, "{toId}", String.valueOf(subscribeDto.getToId()));
        }
        if (subscribeDto.getPageArr() != null && subscribeDto.getPageArr().length > 0) {
            pageStr = String.format(pageStr, subscribeDto.getPageArr());
        }
        return pageStr;
    }

    public String getData(WxSubscribeDto subscribeDto) {
        String dataStr = data;
        if (StringUtils.contains(dataStr, "{toId}")) {
            dataStr = StringUtils.replaceAll(dataStr, "{toId}", String.valueOf(subscribeDto.getToId()));
        }
        if (subscribeDto.getDataArr() != null && subscribeDto.getDataArr().length > 0) {
            dataStr = String.format(dataStr, subscribeDto.getDataArr());
        }
        return dataStr;
    }

    private static Map<Integer, WxSubscribeTypeEnum> map = EnumUtil.getEnumMap(values(), WxSubscribeTypeEnum::getId);

    public static WxSubscribeTypeEnum getById(Integer id) {
        return map.get(id);
    }
}
